package com.watson.http;

import android.app.Dialog;
import android.content.Intent;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.watson.LoginActivity;
import com.watson.MainActivity;
import com.watson.MyApp;
import com.watson.R;
import com.watson.utils.LogUtils;
import com.watson.utils.ToastUtil;

import java.net.ConnectException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;

public abstract class HttpCallbackData extends AbsCallback<JsonBeanData> {
    private Dialog mLoadingDialog;
    //10001,10002,10003

    @Override
    public JsonBeanData convertResponse(okhttp3.Response response) throws Throwable {
        return JSON.parseObject(response.body().string(), JsonBeanData.class);
    }

    @Override
    public void onSuccess(Response<JsonBeanData> response) {
        LogUtils.e(response.body() + "");
        JsonBeanData bean = response.body();
        if (bean != null) {
            if (bean.getCode() == 10002 || bean.getCode() == 10001) {//令牌错误
                ToastUtil.show(bean.getMsg());
                Intent intent = new Intent(MyApp.getContext(), LoginActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                MyApp.getContext().startActivity(intent);
            }
            if (bean.getCode() == 10003) {//失效刷新
                ToastUtil.show(bean.getMsg());
                MainActivity.get_WMS_RefreshToken();
            } else {
                onSuccess(bean.getCode(), bean.getMsg(), bean.getData());
            }
        } else {
            LogUtils.e("服务器返回值异常--->bean = null");
        }
    }

    @Override
    public void onError(Response<JsonBeanData> response) {
        LogUtils.e(response.body() + "");
        Throwable t = response.getException();
        LogUtils.e("网络请求错误---->" + t.getClass() + " : " + t.getMessage());
        if (t instanceof SocketTimeoutException || t instanceof ConnectException || t instanceof UnknownHostException || t instanceof UnknownServiceException || t instanceof SocketException) {
            ToastUtil.show(R.string.load_failure);
        }
        if (showLoadingDialog() && mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
        onError();
    }

    public void onError() {

    }


    public abstract void onSuccess(int code, String msg, String info);

    @Override
    public void onStart(Request<JsonBeanData, ? extends Request> request) {
        onStart();
    }

    public void onStart() {
        if (showLoadingDialog()) {
            if (mLoadingDialog == null) {
                mLoadingDialog = createLoadingDialog();
            }
            mLoadingDialog.show();
        }
    }

    @Override
    public void onFinish() {
        if (showLoadingDialog() && mLoadingDialog != null) {
            mLoadingDialog.dismiss();
        }
    }

    public Dialog createLoadingDialog() {
        return null;
    }

    public boolean showLoadingDialog() {
        return false;
    }

}
